package com.fmi.cloud.act;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fmi.cloud.app.YcApp;
import com.fmi.cloud.biz.CloudHttpBiz;
import com.fmi.cloud.biz.FmHttpBiz;
import com.fmi.cloud.biz.FmUrl;
import com.fmi.cloud.biz.KxHttpBiz;
import com.fmi.cloud.util.CountDownHandler;
import com.fmi.cloud.util.IntentHelper;
import com.fmi.cloud.R;
import com.fmi.cloud.util.OrangeDataHandler;
import com.fmi.cloud.util.OrangeErrorHandler;
import com.fmi.cloud.util.StringUtil;
import com.fmi.cloud.util.URLHelper;
import com.fmi.cloud.util.VolleyHttpUtil;
import com.fmi.cloud.util.YcToastHelper;
import com.fmi.cloud.view.LoadDialog;

import org.json.JSONObject;

import java.util.List;

public class LoginActivity extends Activity {

    TextView tvLogin;
    TextView tvVerify;
    EditText etVerify;
    EditText etTel;
    private CountDownHandler countDownHandler;
    private String type;

    private void startTimer() {
        if (countDownHandler == null) {
            countDownHandler = new CountDownHandler();
            countDownHandler.isOnlySecond(true);
            countDownHandler.isUseCustomCountDownInfo(true);
            countDownHandler.setOnCountDownTimerFinishedListener(new CountDownHandler.OnCountDownTimerFinishedListener() {
                @Override
                public void onFinish() {
                    enableSmsBtn();
                }

                @Override
                public void onCountDown(String currentSecond) {
                    setCountDownViewOnCounting(currentSecond);
                }
            });
        }
        countDownHandler.startTimer("60");
    }

    private void setCountDownViewOnCounting(String currentSecond) {
        tvVerify.setText(getResources().getString(R.string.count_down_info, currentSecond));
        etVerify.setHint("请输入短信验证码");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);
        type = FmHttpBiz.getType(this);
        tvLogin = (TextView) findViewById(R.id.tvLogin);
        tvVerify = (TextView) findViewById(R.id.tvVerify);
        etVerify = (EditText) findViewById(R.id.etVerify);
        etTel = (EditText) findViewById(R.id.etTel);
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestLogin();
            }
        });
        if (YcApp.TYPE_BROKER.equals(type)) {
            findViewById(R.id.tvReg).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentHelper.jump(LoginActivity.this, MainActivity.class, "url", ((YcApp) getApplication()).getHost() + getResources().getString(R.string.path_reg));
                }
            });
        }
        tvVerify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestCheck();
            }
        });
    }

    private void jumpToReg() {

    }

    private void requestLogin() {
        String phone = etTel.getText().toString();
        String verify = etVerify.getText().toString();
        if (YcApp.TYPE_BROKER.equals(type)) {
            KxHttpBiz.login(LoginActivity.this, phone, verify);
        } else if (YcApp.TYPE_OPERATOR.equals(type)) {
            CloudHttpBiz.login(LoginActivity.this, phone, verify);
        }
    }

    private void requestVerify(String tel, boolean isCloud) {
        if (!StringUtil.isBlank(tel)) {
            disableSmsBtn();
            if (isCloud) {
                CloudHttpBiz.sendSms(this, tel, new VolleyHttpUtil.ResponseCallBack() {
                    @Override
                    public void handleResponse(JSONObject response, int errCode) {
                        handleResp(response);
                    }
                });
            } else {
                KxHttpBiz.sendSms(this, tel, new VolleyHttpUtil.ResponseCallBack() {
                    @Override
                    public void handleResponse(JSONObject response, int errCode) {
                        handleResp(response);
                    }
                });
            }
        } else {
            YcToastHelper.getInstance()._toast("请先输入手机号");
        }
    }

    private void handleResp(JSONObject response) {
        LoadDialog.dismiss(LoginActivity.this);
        boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response, true);
        if (isOk) {
            startTimer();
            YcToastHelper.getInstance()._toast("获取验证码成功");
        } else {
            enableSmsBtn();
        }
    }

    private void requestCheck() {
        final String phone = etTel.getText().toString();
        if (YcApp.TYPE_OPERATOR.equals(type)) {
            CloudHttpBiz.checkExist(LoginActivity.this, phone, new VolleyHttpUtil.ResponseCallBack() {
                @Override
                public void handleResponse(JSONObject response, int errCode) {
                    boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response, true);
                    if (isOk) {
                        List<Object> list = OrangeDataHandler.getList(response);
                        if (list != null && list.size() > 0) {
                            requestVerify(phone, true);
                        } else {
                            YcToastHelper.getInstance()._toast("账户不存在");
                        }
                    }
                }
            });
        } else {
            requestVerify(phone, false);
        }
    }

    private void enableSmsBtn() {
        tvVerify.setEnabled(true);
        tvVerify.setText("获取验证码");
    }

    private void disableSmsBtn() {
        etVerify.setText("");
        tvVerify.setEnabled(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownHandler != null) {
            countDownHandler.stopTimer();
        }
    }

}

